﻿mapl.quickSearch = {
	container:[],
	sequence:null,
	box:null,
	init:function(){
	    var ram = $('recent-actions-module');
	    var filmod = new Element('div', { id: 'module-filter-module','class': 'module' });
	    var head = new Element('h2').set('text','Module Search').inject(filmod);
	    this.box = new Element('input', { type: 'text',id: 'module-filter',styles: { margin: '5px' } }).inject(filmod)
	    .addEvent( 'keyup', this.search.bind(this) );
	    filmod.inject(ram.parentNode,'top');
	    this.box.focus();
	    
	    // Assemble list of sliders
	    var divs = $('content-main').getElements('.module');
	    for (var i=0; i < divs.length; i++) {
	        var div = divs[i];
	        var fx = new Fx.Morph(div, {duration: 500});
	        var names = div.getElement('caption').get('text')
	        $each(div.getElements('a'),function(s){
	        	names += '|' + s.get('text')
	        });
	        this.container.push({ names:names, div:div, fx:fx });
	    }
	},
	
	search:function(){
		this.sequence = new Chain();
		$each(this.container,function(s){
		    regex = new RegExp(this.box.value, 'i');
		    if (regex.test(s.names)) {
		        s.fx.cancel();
		        this.hide(s.div,'block');
		        s.div.setStyles({opacity:1, height:""});
		    }
		    else {
		        s.fx.cancel();
		        this.sequence.chain(function(fx){
			        fx.start({opacity:0, height:0}).chain(this.hide.pass([s.div,'none']));
		        }.pass(s.fx,this));
		    }
		}.bind(this));	
		var runChain = function() { 
			this.sequence.callChain();
			if (this.sequence.length == 0) { runChain = $clear(timer); } 
		}.bind(this);
		var timer = runChain.periodical(50);
	},
	
	hide:function(el,val){
        el.style.display = val;
	}
}
window.addEvent('domready',mapl.quickSearch.init.bind(mapl.quickSearch));